//
//  ViewBuilder.swift
//  EasyCoding
//
//  Created by Fanxx on 2019/5/30.
//  Copyright © 2019 Fanxx. All rights reserved.
//

import UIKit
import SnapKit

@resultBuilder
public struct ECViewBuilder {
    public static func buildBlock(_ segments: ECViewBuildable...) -> ECViewBuildable {
        return ECViewBuildItemArray(items: segments)
    }
    public static func buildIf(_ item: ECViewBuildable?) -> ECViewBuildable  {
        return item ?? ECViewBuildItemNone()
    }
    public static func buildEither(first: ECViewBuildable) -> ECViewBuildable {
        return first
    }
    public static func buildEither(second: ECViewBuildable) -> ECViewBuildable {
        return second
    }
    public static func buildArray(_ components: [ECViewBuildable]) -> ECViewBuildable {
        return ECViewBuildItemArray(items: components)
    }
}

extension EasyCoding where Base: UIView {
    ///插入子视图并返回子视图
    @discardableResult
    public func insert<ViewType: UIView>(_ view:ViewType, at index: Int, layout: ECViewLayout...) -> ECNamespaceWrapper<ViewType> {
        return self.insert(view, at: index, layouts: layout)
    }
    ///插入子视图并返回子视图
    @discardableResult
    public func insert<ViewType: UIView>(_ view:ViewType, at index: Int, layouts: [ECViewLayout]) -> ECNamespaceWrapper<ViewType> {
        self.base.insertSubview(view, at: index)
        layouts.apply(to: view, refer: self.base)
        return view.easy
    }
    ///添加子视图并返回子视图
    @discardableResult
    public func add<ViewType: UIView>(_ view:ViewType, layout: ECViewLayout...) -> ECNamespaceWrapper<ViewType> {
        return self.add(view, layouts: layout)
    }
    ///添加子视图并返回子视图
    @discardableResult
    public func add<ViewType: UIView>(_ view:ViewType, layouts: [ECViewLayout]) -> ECNamespaceWrapper<ViewType> {
        self.base.addSubview(view)
        layouts.apply(to: view, refer: self.base)
        return view.easy
    }
    ///添加到父视图并返回子视图，相对于自己布局
    @discardableResult
    public func addToSuper<ViewType: UIView>(_ view:ViewType, layout: ECViewLayout...) -> ECNamespaceWrapper<ViewType> {
        return self.addToSuper(view, layouts: layout)
    }
    ///添加到父视图并返回子视图，相对于自己布局
    @discardableResult
    public func addToSuper<ViewType: UIView>(_ view:ViewType, layouts: [ECViewLayout]) -> ECNamespaceWrapper<ViewType> {
        self.base.superview?.addSubview(view)
        layouts.apply(to: view, refer: self.base)
        return view.easy
    }
    ///构建子视图并返回自身
    @discardableResult
    public func build(@ECViewBuilder _ builder: () -> ECViewBuildable ) -> Self {
        let items = builder()
        //先把所有视图都加好之后再统一设约束
        if let stack = self.base as? UIStackView {
            items.__easyPerformBuild(add: stack.addArrangedSubview(_:))
        }else{
            items.__easyPerformBuild(add: self.base.addSubview(_:))
        }
        items.__easyPerformBuildLayout()
        return self
    }
}

#if EASY_SHORTCUT

extension UIView {
    ///插入子视图并返回子视图
    @discardableResult
    public func insert<ViewType: UIView>(_ view:ViewType, at index: Int, layout: ECViewLayout...) -> ViewType {
        return self.insert(view, at: index, layouts: layout)
    }
    ///插入子视图并返回子视图
    @discardableResult
    public func insert<ViewType: UIView>(_ view:ViewType, at index: Int, layouts: [ECViewLayout]) -> ViewType {
        self.insertSubview(view, at: index)
        layouts.apply(to: view, refer: self)
        return view
    }
    ///添加子视图并返回子视图
    @discardableResult
    public func add<ViewType: UIView>(_ view:ViewType, layout: ECViewLayout...) -> ViewType {
        return self.add(view, layouts: layout)
    }
    ///添加子视图并返回子视图
    @discardableResult
    public func add<ViewType: UIView>(_ view:ViewType, layouts: [ECViewLayout]) -> ViewType {
        self.addSubview(view)
        layouts.apply(to: view, refer: self)
        return view
    }
    ///添加到父视图并返回子视图，相对于自己布局
    @discardableResult
    public func addToSuper<ViewType: UIView>(_ view:ViewType, layout: ECViewLayout...) -> ViewType {
        return self.addToSuper(view, layouts: layout)
    }
    ///添加到父视图并返回子视图，相对于自己布局
    @discardableResult
    public func addToSuper<ViewType: UIView>(_ view:ViewType, layouts: [ECViewLayout]) -> ViewType {
        self.superview?.addSubview(view)
        layouts.apply(to: view, refer: self)
        return view
    }
    ///构建子视图并返回自身
    @discardableResult
    public func build(@ECViewBuilder _ builder: () -> ECViewBuildable ) -> Self {
        let items = builder()
        //先把所有视图都加好之后再统一设约束
        if let stack = self as? UIStackView {
            items.__easyPerformBuild(add: stack.addArrangedSubview(_:))
        }else{
            items.__easyPerformBuild(add: self.addSubview(_:))
        }
        items.__easyPerformBuildLayout()
        return self
    }
}

#endif
